package com.example.societiesSystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.societiesSystem.common.R;
import com.example.societiesSystem.entity.User;
import com.example.societiesSystem.mapper.UserMapper;
import com.example.societiesSystem.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.societiesSystem.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gl
 * @since 2025-05-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public R login(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, user.getUserName());
        queryWrapper.eq(User::getPassWord, user.getPassWord());
        User data = this.getBaseMapper().selectOne(queryWrapper);
        if (data != null) {
            return R.messageData("登录成功！",data);
        }
        return R.errorMessage("用户名或密码错误！");
    }

    @Override
    public R emailLogin(Map<String, String> map) {
        if (!redisUtil.hasKey(map.get("email"))) {
            return R.errorMessage("验证码已过期");
        }
        String code = (String) redisUtil.get(map.get("email"));
        if (code.equals(map.get("code"))) {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getEmail, map.get("email"));
            User user = this.getBaseMapper().selectOne(queryWrapper);
            if (user != null){
                return R.okMessage("登录成功！");
            }
        }
        return R.errorMessage("登录失败！");
    }

    @Override
    public IPage<User> pageAll(Long pageIndex, Long pageSize, String userName, String name) {
        IPage<User> userPage = new Page<>(pageIndex,pageSize);
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (CharSequenceUtil.isNotBlank(userName)) {
            userLambdaQueryWrapper.like(User::getUserName, userName);
        }
        if (CharSequenceUtil.isNotBlank(name)) {
            userLambdaQueryWrapper.like(User::getName, name);
        }
        return this.page(userPage, userLambdaQueryWrapper);

    }

    @Override
    public boolean batchInsert(List<User> list) {
        return this.saveBatch(list);
    }

    @Override
    public String getNameById(Long id) {
        List<User> users = this.getBaseMapper().selectList(new LambdaQueryWrapper<User>().eq(User::getId, id));
        if (CollUtil.isNotEmpty(users)){
            return users.get(0).getName();
        }
        return "";
    }
}
